package springboot.com.utils.jpa;

import java.util.List;

import org.apache.commons.lang3.StringUtils;


/**
 * 
 */
public class PagingVO {

	private String pageIndex;

	private String pageSize;

	/**
	 * aa,asc|bb,desc
	 */
	private String orderName;

	private SqlOrder order;

	private List<?> details;

	private Long count;

	private Long pageCount;

	public PagingVO() {
		this.pageIndex = "1";
		this.pageSize = "10";
	}
	
	

	public PagingVO(int pageSize) {
		super();
		this.pageSize = Integer.toString(pageSize);
	}



	public String getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getOrderName() {
		if (StringUtils.isNotBlank(orderName)) {

			if (orderName.contains(",")) {
				return orderName.split(",")[0];
			} else {
				return orderName;
			}

		} else {
			return "";
		}

	}

	public void setOrderName(String orderName) {
		if (StringUtils.isNotBlank(orderName)) {

			if (orderName.contains(",")) {
				this.orderName = orderName.split(",")[0];
				this.order =SqlOrder.valueOf(orderName.split(",")[1]);
			} else {
				this.orderName = orderName;
			}
		}
	}

	public SqlOrder getOrder() {
		
		if(StringUtils.isNotBlank(orderName)){
			
			if(orderName.contains(SqlOrder.asc.name()) ){
				
				int i=orderName.indexOf(SqlOrder.asc.name());
				return SqlOrder.valueOf(orderName.substring(i));
				
			}
			
			if( orderName.contains(SqlOrder.desc.name())){
				
				int i=orderName.indexOf(SqlOrder.desc.name());
				return SqlOrder.valueOf(orderName.substring(i));
			}
		}
		return order;
	}

	public void setOrder(SqlOrder order) {
		this.order = order;
//		if(StringUtils.isNotBlank(order.toString())){
//			if (orderName.contains(",")) {
//				this.order =SqlOrder.valueOf(orderName.split(",")[1]);
//			} else {
//				this.order = order;
//			}
//		}
	}

	public List<?> getDetails() {
		return details;
	}

	public void setDetails(List<?> details) {
		this.details = details;
	}

	public Long getCount() {
		return count;
	}

	public void setCount(Long count) {
		this.count = count;
		if (pageSize != null && this.count != null) {
			Long size = Long.parseLong(pageSize);
			if (size != null && size > 0) {
				this.pageCount = (count / size) + ((count % size) == 0 ? 0 : 1);
			}
		}
	}

	public Long getPageCount() {
		return pageCount;
	}

	public void setPageCount(Long pageCount) {
		this.pageCount = pageCount;
	}

}
